﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StuRecruitSysDAL;
using System.Threading;
using StuRecruitSysBLL;
using StuRecruitSysModels;
using System.Net.Mail;

namespace StuRecruitSys.page
{

   
    public partial class Login : System.Web.UI.Page
    {
        StudentService stus=new StudentService();
        LoginManager logma = new LoginManager();
        Student student = new Student();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
   
            }
        }

        protected void login_click(object sender, EventArgs e)
        {
           
            String u_name= username.Text.Trim();
            String u_pasd = password1.Value;
            String u_type = type.Text.Trim();

            if (u_name == "")
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("test"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('用户名不能为空!');</script>");
                }
         
                return;
            }
            if (u_pasd == "")
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("test"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('密码不能为空!');</script>");
                }
                return;
            }           
            else
            {
                if (u_type == "学生")
                {
                    string pwd = logma.StudentLogin(u_name).Spwd;
                    if (pwd == u_pasd)
                    {
                        Response.Redirect("StudentHome.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('用户名或者密码错误!');</script>");
                    }
                }
                else
                {
                    string pwd = logma.CashierLogin(u_name).Cpwd;
                    if (pwd == u_pasd)
                    {
                        Response.Redirect("CashierHome.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('用户名或者密码错误!');</script>");
                    }
                } 
            }
        }

        protected void linkRegiste_click(object sender, EventArgs e)
        {
            Response.Redirect("Registe.aspx");
        }
        //忘记密码
        public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
        }
        //第一个参数是邮箱服务器
        //第二个参数发件人的帐号
        //第三个参数发件人密码
        //第四个参数收件人帐号
        //第五个参数主题
        //第六个参数内容.
        protected void btnForgetPwd_click(object sender, EventArgs e)
        {
            LoginManager loginManager = new LoginManager();
            student.Username = username.Text.Trim();
            student.Spwd = loginManager.StudentLogin(student.Username).Spwd;
            student.Email = loginManager.StudentLogin(student.Username).Email;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert(student.Username+ student.Spwd+student.Email );</script>");
            try
            {
                SendSMTPEMail("smtp.qq.com", student.Email, "填写你qq邮箱的密码", "填写你要发送对方的QQ邮箱账户", "招生系统.net邮件发送测试", "这是用asp.net发送邮件，用qq的smtp.qq.com服务器，测试成功");

            }
            catch (Exception)
            {

            }
        } 
    }
}